package cn.jiyun.provider.service.impl;

import cn.jiyun.provider.dao.DeptDao;
import cn.jiyun.provider.dao.UserDao;
import cn.jiyun.provider.pojo.Dept;
import cn.jiyun.provider.pojo.User;
import cn.jiyun.provider.pojo.UserDto;
import cn.jiyun.provider.service.UserService;
import cn.jiyun.provider.utils.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private DeptDao deptDao;

    @Override
    public Page page(Integer current,String name) {
        Page<User> page = new Page<>();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,User::getName,name);
        Page<User> userPage = userDao.selectPage(page,queryWrapper);
        return userPage;
    }

    @Override
    public int add(User user) {
        int i = userDao.insert(user);
        return i;
    }

    @Override
    public User getById(Long id) {
        log.info("=================>id:{}",id);
        return userDao.selectById(id);
    }

    @Override
    public int edit(User user) {
        return userDao.updateById(user);
    }

    @Override
    public int del(Long id) {
        return userDao.deleteById(id);
    }

    @Override
    public void excelFile(HttpServletResponse response) {
        String[] tou={"姓名","电话","性别","年龄","状态","图片","创建时间"};
        String[] ziduan={"name","phone","sex","age","status","avatar","createTime"};
        List<User> users = userDao.selectList(null);
        if(CollectionUtils.isEmpty(users)){
            log.info("集合为空:{}",users.toArray());
        }
        LinkedHashMap<String, String> map = new LinkedHashMap<>();

        for (int i = 0; i < ziduan.length; i++) {
            map.put(ziduan[i],"");
        }
        ExcelUtil.exportExcel(users.size(),response,users,tou,map);

    }

    @Override
    public List<Dept> depts() {
        return deptDao.selectList(null);

    }
}
